package initialize

import (
	"fmt"
	"ksirGo/global"

	"github.com/fsnotify/fsnotify"
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
)

func Viper() *viper.Viper {
	v := viper.New()
	mode := gin.Mode() // debug test release

	v.SetConfigFile("config/config." + mode + ".yml")
	v.SetConfigType("yaml")

	err := v.ReadInConfig()
	if err != nil {
		panic(fmt.Errorf("配置文件错误: %s", err))
	}
	v.WatchConfig()

	v.OnConfigChange(func(e fsnotify.Event) {
		fmt.Println("config file changed:", e.Name)
		if err := v.Unmarshal(&global.CONFIG); err != nil {
			fmt.Println(err)
		}
	})
	if err := v.Unmarshal(&global.CONFIG); err != nil {
		fmt.Println(err)
	}
	// fmt.Println("配置内容：", global.CONFIG)
	return v
}
